﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeManageService;

namespace WeManageForm
{
    public partial class WishListForm : Form
    {
        public WishListForm()
        {
            InitializeComponent();
            DGVWish.DataSource = BSWish;
            BSWish.DataSource = WishManager.wishes;
			btnSelectAll.Visible = false;
			btnDelete.Visible = false;
			DGVWish.CellContentClick += WishDetailBtnClick;
			DGVWish.CellContentClick += WishCheckClick;
			RefreshData();
        }

		private void RefreshData()
		{
			BSWish.ResetBindings(false);
			UserDBManager userDBManager = new UserDBManager();
			User user = userDBManager.GetUser(Global.UserName);
			lblScore.Text = user.Score.ToString();
		}

		private void btnAddWish_Click(object sender, EventArgs e)
        {
            WeManageForm.Wish.WishForm wishForm = new Wish.WishForm();
            wishForm.StartPosition = FormStartPosition.CenterScreen;
            wishForm.ShowDialog();
			RefreshData();
		}

        private void btnTurnOver_Click(object sender, EventArgs e)
        {
            Wish.TurnoverForm turnoverForm = new Wish.TurnoverForm();
            turnoverForm.StartPosition = FormStartPosition.CenterScreen;
            turnoverForm.ShowDialog();
        }

		private void btnManage_Click(object sender, EventArgs e)
		{
			if(btnManage.Text == "管理")
			{
				btnManage.Text = "取消";
				foreach (DataGridViewColumn col in DGVWish.Columns)
				{
					if (col.Name == "check") col.Visible = true;
				}
				btnSelectAll.Visible = true;
				btnDelete.Visible = true;
			}
			else
			{
				btnManage.Text = "管理";
				foreach (DataGridViewColumn col in DGVWish.Columns)
				{
					if (col.Name == "check") col.Visible = false;
				}
				btnSelectAll.Visible = false;
				btnDelete.Visible = false;
			}
		}

		private void btnSelectAll_Click(object sender, EventArgs e)
		{
			int colIndex = -1;
			foreach (DataGridViewRow row in DGVWish.Rows)
			{
				if (colIndex == -1)
				{
					for (int i = 0; i < row.Cells.Count; i++)
					{
						if (row.Cells[i].OwningColumn.Name == "check")
						{
							colIndex = i;
						}
					}
				}
				if (btnSelectAll.Text == "全选") row.Cells[colIndex].Value = true;
				else row.Cells[colIndex].Value = false;
			}
			if (btnSelectAll.Text == "全选") btnSelectAll.Text = "取消";
			else btnSelectAll.Text = "全选";
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			bool? check;
			int? id;
			foreach (DataGridViewRow i in DGVWish.Rows)
			{
				check = null;
				id = null;
				foreach (DataGridViewCell j in i.Cells)
				{
					if (j.OwningColumn.Name == "check")
					{
						check = j.Value as bool?;
					}
					else if (j.OwningColumn.Name == "WishID")
					{
						id = j.Value as int?;
					}
					if (check != null && id != null)
					{
						if ((bool)check) WishManager.DeleteWish((int)id);
						break;
					}
				}
			}
			RefreshData();
		}
		
		private void WishDetailBtnClick(object sender, DataGridViewCellEventArgs e)
		{
			if (DGVWish.Columns[e.ColumnIndex].Name == "DetailBtn")
			{
				if (e.RowIndex == -1) return;
				foreach (DataGridViewCell cell in DGVWish.Rows[e.RowIndex].Cells)
				{
					if (cell.OwningColumn.DataPropertyName == "WishID")
					{
						WeManageService.Wish wish = WishManager.GetWishClone((int)cell.Value);
						Wish.WishForm form = new Wish.WishForm(wish);
						form.Closed += (_1, _2) => RefreshData();
						form.ShowDialog();
						break;
					}
				}
			}
		}

		private void WishCheckClick(object sender, DataGridViewCellEventArgs e)
		{
			if (DGVWish.Columns[e.ColumnIndex].Name == "check" && e.RowIndex != -1)
				btnSelectAll.Text = "全选";
		}

    }
}
